package dper.study.codes.concurrent.worker;

import dper.study.codes.concurrent.model.ParellelResult;
import dper.study.codes.concurrent.model.ParellelType;
import dper.study.codes.concurrent.utils.ThreadSafeSimpleDateFormatUtil;
import org.apache.log4j.Logger;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;

/**
 * Created by leichen on 2017/3/21.
 */
public class DateFormatWorker implements Callable<ParellelResult> {
    Logger logger = Logger.getLogger(DateFormatWorker.class) ;

    public ParellelResult call() throws Exception {
        ParellelResult ret = new ParellelResult() ;

        Random rand = new Random();
        List<String> retDate = new ArrayList<String>() ;
        for (int i=0;i<10;i++){
            Thread.sleep(Math.abs(rand.nextInt()) % 1000 + 500);
            String d = ThreadSafeSimpleDateFormatUtil.getDateFormat().format(new Date()) ;
            int c = ThreadSafeSimpleDateFormatUtil.getThreadLocalInt() ;
            c ++ ;
            ThreadSafeSimpleDateFormatUtil.setThreadLocalInt(c);
            retDate.add(d) ;
            Thread.yield();
        }
        logger.info(Thread.currentThread() + " ThreadLocalInt " + ThreadSafeSimpleDateFormatUtil.getThreadLocalInt());
        ret.setSuccess(true);
        ret.setResult(retDate);
        ret.setParellelType(ParellelType.PT_DATE_FORMAT);
        return  ret ;
    }
}
